#include "url.h"
#include "simplesocket.h"
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <pthread.h>
#include <queue>
#include <set>
#define DEBUG (1)

using namespace std;

queue<url_t> * url_queue;
queue<char*> * buffer_queue;
set<url_t, lturl> * parsed_urls;

const string get_page (const url_t& url) {
    char buf[MAX_READ];
    clientsocket sock (url.host.c_str(), 80, 0, false);
    if(sock.connect()){
        sprintf(buf, "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", url.file.c_str(), url.host.c_str());
        sock.write(buf, strlen(buf));

        int ret;
        int size = 0;
        sock.setTimeout(5);

        while((ret = sock.read(buf+size, MAX_READ-1-size)) > 0){
            size += ret;
        }
    }
    sock.close();

    return string(buf);
}

int main (int argc, char** argv) {
    if(argc < 4){
        printf("You should call the program like this:\n");
        printf("spider <url> <depth> <threads>\n");
        printf("Dont include <>s. url is a string, depth + threads are ints.");
        return 0;
    }

    url_queue   = new queue<url_t>();
    parsed_urls = new set<url_t, lturl>();

    const string root_url_str      = argv[1];
    const unsigned int max_depth   = atoi(argv[2]);
    const unsigned int max_threads = atoi(argv[3]);
    url_t root_url;
    parse_single_URL(root_url_str, root_url.host, root_url.file);

    if((DEBUG)){
        printf("Root URL:               %s\n", root_url_str.c_str());
        printf("Root Host:              %s\n", root_url.host.c_str());
        printf("Root File:              %s\n", root_url.file.c_str());
        printf("Maximum crawl depth:    %u\n", max_depth);
        printf("Maximum worker threads: %u\n", max_threads);
    }

    cout << get_page(root_url) << endl;
    
    //parse_URLs(testbuf, strlen(testbuf), urls_set);

    delete url_queue;
    delete parsed_urls;
    return (EXIT_SUCCESS);
}